package com.trkj.push;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.shamanland.fonticon.FontIconDrawable;
import com.trkj.base.BaseActivity;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.ProgressBarDataFromRemote;
import com.trkj.base.newwork.WaitingDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * Created by LiZanhong on 2015/07/07.
 */
public class NotificationActivity extends BaseActivity{
    public static final String ACTION = "com.trkj.push.NotificationActivity";

    @ViewInject(R.id.list)
    private ListView listView;

    private JSONArray data;

    class NotificationAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.notification_item, null);
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                viewHolder.status = (ImageView) convertView.findViewById(R.id.status);
                viewHolder.time = (TextView) convertView.findViewById(R.id.time);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            JSONObject obj = data.getJSONObject(position);
            int status = obj.getIntValue("status");
            viewHolder.title.setText(obj.getString("titile"));
            viewHolder.content.setText(obj.getString("content"));
            if(status == 0) {
                viewHolder.status.setImageDrawable(FontIconDrawable.inflate(
                        NotificationActivity.this, R.xml.icon_unread));
            }else{
                viewHolder.status.setImageDrawable(FontIconDrawable.inflate(
                        NotificationActivity.this, R.xml.icon_read));
            }
            viewHolder.time.setText("接收时间：" + obj.getString("create_date"));

            hadRead(obj.getString("m_id"), status);

            return convertView;
        }

        class ViewHolder{
            TextView title;
            TextView content;
            ImageView status;
            TextView time;
        }
    }

    /**
     * 将看过的消息设置为已读状态
     * @param md5
     * @param status
     */
    protected void hadRead(String md5, int status){
        if(status == 1) return;

        WaitingDataFromRemote dataFromRemote = new WaitingDataFromRemote(this);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {

            }
        });

        Integer uid = UserService.getInstance(this).parseUser().userId;
        String url = MessageFormat.format(Constants.ApiUrl.PUSH_MESSAGE_STATUS_CHANGED, uid, md5);
        dataFromRemote.execute(url);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_common);

        ViewUtils.inject(this);

        ProgressBarDataFromRemote dataFromRemote =
                new ProgressBarDataFromRemote(this, new OnResponseResultListener() {
                    @Override
                    public void onResponseResult(String result) {
                        data = JSON.parseObject(result).getJSONArray("data");
                        listView.setAdapter(new NotificationAdapter());
                    }
                });

        Integer uid = UserService.getInstance(this).parseUser().userId;
        if(uid != null) {
            String url = MessageFormat.format(Constants.ApiUrl.PUSH_MESSAGE_LISH, uid);
            dataFromRemote.execute(url);
        }
    }



    @Override
    protected String getCustomerTitle() {
        return "推送消息";
    }
}
